directives.directive('dashCard', ['$rootScope','tools',function ($rootScope,tools) {
	return {
		restrict: 'E',
		scope: {
            cardsconfig:'='
        },
        replace: true,
        css:'app/pages/dashBroad/dashCardDire/dashCard.css',
        templateUrl:'app/pages/dashBroad/dashCardDire/dashCardView.html',
		link: function (scope, element, iAttrs) {

            console.log(scope.cardsconfig);

			var evs = {};
            var catName = "";

            // 所有一级类目的统计数据
            scope.whole_stat_data = {};
            scope.date = "";

            // 当前一级类目的统计数据
            scope.cur_cat_stat_data = {
                averagePrice: 0,
                sellAmount: 0,
                sellMoney: 0,
                shopNum: 0
            };

            evs.GLOBAL_CATEGORY_CHANGE_EV_OFF = $rootScope.$on('GLOBAL_CATEGORY_CHANGE_EV', function (e, catName_) {
                console.log('GLOBAL_CATEGORY_CHANGE_EV')
                scope.cur_cat_stat_data = {
                    averagePrice: 0,
                    productNum: 0,
                    sellAmount: 0,
                    sellMoney: 0,
                    shopNum: 0
                };
                
                tools.get({
                    url: '/marketconditions/statistic/getCategoryStatistic.htm',
                    data: {
                        catName: catName_
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            catName = catName_;

                            scope.cur_cat_stat_data = resp.value[0];
                            scope.cur_cat_stat_data.date = tools.format(scope.cur_cat_stat_data.date);
                            scope.cur_cat_stat_data.averagePrice = (scope.cur_cat_stat_data.sellMoney / scope.cur_cat_stat_data.sellAmount).toFixed(2);
                        }
                    }
                });
            });

            scope.$on('$destroy', function () {
                for (var i in evs) {
                    if (i.indexOf('_OFF') != -1) {
                        evs[i]();
                    }
                }
            });
		}
	};
}])